package com.china08.yxyapi.api.manage.stu.mystudy.homework.dowork;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.manage.stu.mystudy.homework.dowork.impl.HomeworkServiceImpl;

/***
 * 学生每答一道题提交的接口 2017 年3月10日09:04:39**
 * 
 * @author lys
 *
 */

@CrossOrigin
@RestController
public class WorkPostController extends YxyApiBaseController {
    @Autowired
    HomeworkServiceImpl homeworkSrevice;

    @Autowired
    private HomeworkTaskReportRepository htrr;

    /**
     * 学生回答每一道题的接口，id为题ID，参数：题ID+学生答案 duration 为做题时长
     *
     * @param id
     *            作业id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/homework/dowork/work", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel process(@RequestBody ReqModel reqModel, HttpServletRequest request,
            HttpServletResponse response) {
        // 获取当前用户信息
        YxyProfile yxyProfile = getProfile(request, response);
        Assert.isTrue(yxyProfile != null, "用户信息异常，请重新登录！");
        // 获取任务信息
        // 如果任务类型为2 且已经提交过作业操作那么禁止用户进行提交当前题
        HomeworkTaskReport homeworkTaskReport = htrr.findByStudentIdAndTaskId(reqModel.studentId, reqModel.taskId);
        if ("2".equals(homeworkTaskReport.getTaskType())) {
            Assert.isTrue(homeworkTaskReport.getQuesOfFinished() != 1, "当前任务下的试题作业已提交！");
        }
        // 设置默认返回值
        RespModel respModel = new RespModel();
        respModel.setMessage("提交失败");
        // 得到参数并验证
        Assert.notNull(StringUtils.defaultIfBlank(reqModel.id, null), "作业的ID不能为空");
        Assert.notNull(StringUtils.defaultIfBlank(reqModel.taskId, null), "的ID不能为空");
        /* 作业完成情况:-1-逾期未提交,0-未做,1-完成 ,2-已批阅,3-答题中 */
        if (homeworkTaskReport.getStatusOfFinished() == 3 || homeworkTaskReport.getStatusOfFinished() == 0) {
            // 调用service层进行保存
            homeworkSrevice.commitHomeworkForXZ(reqModel.studentId, reqModel.id, reqModel.answerOfMy, reqModel.duration,
                    reqModel.taskId);
            respModel.setMessage("提交成功！");
            respModel.saveResultats = "作业已保存";
        } else if (homeworkTaskReport.getStatusOfFinished() == 1 || homeworkTaskReport.getStatusOfFinished() == 2) {
            respModel.saveResultats = "作业已提交";
        } else if (homeworkTaskReport.getStatusOfFinished() == -1) {
            respModel.saveResultats = "作业已逾期";
        }
        return respModel;

    }

    // 返回的对象类型
    protected class RespModel extends YxyApiRespModel {
        public String saveResultats;
    }

    // 请求的参数类型
    protected static class ReqModel extends YxyApiReqModel {
        // 当前题的ID
        public String id;
        public Integer duration;
        // 答案:我的 ；可能为空，因为主观题为图片链接 <br/> 多选题，使用A，B 存储
        public String answerOfMy;
        // 学生id
        public String studentId;
        // 任务ID
        public String taskId;
    }
}
